package top.szhome.framework.starter.boot.jackson;

import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.PackageVersion;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import top.szhome.framework.core.tools.util.DateUtil;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * Java 8时间序列化
 *
 * @author 唐加利
 * @date 2023/7/24
 **/
public class SzhomeJavaTimeModule extends SimpleModule {

    /**
     * 单例模式
     */
    public static final SzhomeJavaTimeModule INSTANCE = new SzhomeJavaTimeModule();

    /**
     * 构造时间序列化器
     */
    public SzhomeJavaTimeModule() {
        super("szhome.java.time.module", PackageVersion.VERSION);
        this.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateUtil.getDateTimeFormatter(DateUtil.FORMAT_LONG)));
        this.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateUtil.getDateTimeFormatter(DateUtil.FORMAT_DATE)));
        this.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateUtil.getDateTimeFormatter(DateUtil.FORMAT_TIME)));
        this.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateUtil.getDateTimeFormatter(DateUtil.FORMAT_LONG)));
        this.addSerializer(LocalDate.class, new LocalDateSerializer(DateUtil.getDateTimeFormatter(DateUtil.FORMAT_DATE)));
        this.addSerializer(LocalTime.class, new LocalTimeSerializer(DateUtil.getDateTimeFormatter(DateUtil.FORMAT_TIME)));
        this.addSerializer(Long.class, BigNumberSerializer.instance);
    }
}
